package br.ufrn.dimap.Conexao;

import br.ufrn.dimap.Grafo.Comentario;
import br.ufrn.dimap.Grafo.Postagem;
import br.ufrn.dimap.Grafo.Rede;
import br.ufrn.dimap.Grafo.Usuario;
import br.ufrn.dimap.Grafo.excecoes.TokenException;
import java.util.ArrayList;
import java.util.List;

import com.restfb.*;
import com.restfb.FacebookClient.AccessToken;
import com.restfb.types.Comment;
import com.restfb.types.Post;
import com.restfb.types.Post.Comments;
import com.restfb.types.User;
import java.util.HashMap;
import java.util.Iterator;


public class FBConnection {
	FacebookClient fb;
	
	public FBConnection(String at){
		fb = new DefaultFacebookClient(at);	
	}
	
        private List<Postagem> converterPosts(List<Post> posts){
            List<Postagem> postagens = new ArrayList<Postagem>();
            Iterator<Post> itPost = posts.iterator();
            while(itPost.hasNext()){
                Post p = itPost.next();
                if(p.getMessage() != null){
                    System.out.println(p.getMessage());
                    System.out.println(p.getLikesCount());
                    System.out.println(p.getFrom().getName());
                    System.out.println(p.getLink());
                   
                    Postagem postagem = new Postagem(p.getMessage(), p.getLink(), p.getFrom().getName(), p.getLikesCount());
                    Iterator<Comment> itComments = p.getComments().getData().iterator();
                    
                    while(itComments.hasNext()){
                        Comment c = itComments.next();
                        if(c.getMessage() != null)
                            postagem.addComentario(new Comentario(c.getMessage(), c.getCreatedTime().toString(), c.getFrom().getName(), c.getLikeCount()));
                    }
                    postagens.add(postagem);
                }
            }
            return postagens;
        }
        
	public Usuario getUserByName(String nome) throws TokenException{
            try{
		User user = fb.fetchObject(nome, User.class);
                Connection<Post> myFeed = fb.fetchConnection(nome+"/feed", Post.class);        
                Usuario usuario = new Usuario(user.getId(), user.getName(), user.getAbout(), user.getBirthday(), user.getEmail(), user.getGender(), user.getLocale());
                usuario.setPosts(converterPosts(myFeed.getData()));
                //usuario.setPost(myFeed.getData());
                return usuario;
            }catch(Exception e){
                throw new TokenException();
            }
	}
        
        //obter a lista de usuarios com permissao
        public List<Usuario> getFriendsLista(String nome) throws TokenException{
                List<Usuario> amigos = new ArrayList<Usuario>();
                try{
                    Connection<User> myFriends = fb.fetchConnection(nome + "/friends", User.class);
                    Iterator<User> friends = myFriends.getData().iterator();
                    
                    while(friends.hasNext())
                    {
                        User next = friends.next();
                        amigos.add(new Usuario(next.getId(), next.getName()));
                    }
                }
                catch(Exception e){
                    throw new TokenException();
                }
		return amigos;
        }
}